# -*- coding: utf-8 -*-
import scrapy
import urlparse
from tools import my_email


class ImgItem(scrapy.Item):
    src = scrapy.Field()


class HylImagesSpider(scrapy.Spider):
    name = 'Hyl_images'
    allowed_domains = ['ivsky.com']
    start_urls = ['http://www.ivsky.com/tupian/ziranfengguang/']

    def parse(self, response):
        yield scrapy.Request(
            url=response.url,
            callback=self.parse_list,
            # 分布式爬虫不需要
            dont_filter=True,
        )

    def parse_list(self, response):
        img_list = response.xpath("//img/@src").extract()
        for src in img_list:
            src = urlparse.urljoin(response.url, src)
            item = ImgItem()
            item['src'] = src
            yield item

    def parse_detail(self, response):
        pass

    @staticmethod
    def close(spider, reason):
        closed = getattr(spider, 'closed', None)
        print u"爬虫项目关闭"
        my_email.send_file_email("15238356618@163.com",
                              u"<h1>爬取风景图片数据</h1>",
                              "imgs.csv",
                              u"图片数据的附件",
                              )
        if callable(closed):
            return closed(reason)